<?php

namespace app\console\controller;

namespace app\console\controller;

use think\Session;
use think\Url;
use think\Cache;
use think\Loader;
use think\Request;
use think\Controller;
use app\console\model\Ad as AdModel;
use app\console\model\ServiceOrderEvaluate as Model;
use app\common\trilateral\Classify as ClassifyAlias;
use think\Db;

class ServiceOrderEvaluate extends Controller
{


    /**
     * 前置方法
     */
    protected $beforeActionList = [
        'clearCache' => ['only'],
    ];


    /**
     * 广告位列表
     */
    public function index()
    {
        $param = Request::instance()->param();
        $where = [];
        if(!empty($param['title'])) $where['soe.content|m.username'] = ['like','%'.$param['title'].'%'];
        if (!empty($param['interval_timer'])) {
            $interval_timer = explode(' / ', $param['interval_timer']);
            $where['soe.create_time'] = ['between time', [strtotime($interval_timer[0]), strtotime($interval_timer[1])]];
        }

        $data = Model::order(['id' => 'desc'])
            ->alias('soe')
            ->join('member m','m.id = soe.member_id','left')
            ->field('soe.*,m.username')
            ->where($where)
            ->paginate(10,false,['query' => $param])->each(function($item,$key){
                $item->service_type = '';
                if($item->order_type == 1){ //商品
                    $item->service_type = Db::name('shop_goods')->where('id',$item->single_id)->value('title');
                }
                if($item->order_type == 2){ //服务
                    $item->service_type = Db::name('single')->where('id',$item->single_id)->value('cat_name');
                }
                if(session('role_id') != 1){
                    $item->username = hideStr($item->username,1,6);
                }
                if($item->order_type == 3){
                    $t_content = '';
                    if($item->t_type == 2){
                        $t_content = '会员卡';
                        $item->service_type = Db::name('system_card')->where('id',$item->single_id)->value('title').'-'.$t_content;
                    }else{
                        $item->service_type = Db::name('gift')->where('id',$item->single_id)->value('title').'-'.$t_content;
                    }

                }
            });

        return $this->fetch('service_order_evaluate', [
            'data' => $data,
        ]);
    }


    /**
     * 广告位创建
     */
    public function create(Request $request, Model $model)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

//            // 验证
//            $validate = Loader::validate('AdType');
//            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $model->allowField(true)->save($data);

            if ($dispose) {
                win('操作成功', Url::build('index'));
            }

            fail('操作失败');

        }

        $news_type = Db::name('web')->where(['auding_status' => 2,'is_delete' => 1,'parent_id' => 0])->select();
        return $this->fetch('create', [
            "news_type" => $news_type,
        ]);

    }


    /**
     * 广告位编辑
     */
    public function update($id, Request $request, Model $model)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();
            // 验证
           // $validate = Loader::validate('AdType');
           // if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $model->allowField(true)->save($data, ['id' => $id]);


            if ($dispose !== false) {
                win('操作成功', Url::build('index'));
            }

            fail('操作失败');

        }

        $item = Model::get($id);
        $news_type = Db::name('web')->where(['auding_status' => 2,'is_delete' => 1,'parent_id' => 0])->select();
        return $this->fetch('create', [
            "news_type" => $news_type,
            'item' => $item
        ]);
    }



    /**
     * 广告位删除
     */
    public function destroy($id, Model $model)
    {

        if (Request::instance()->isGet()) {
            //Db::name('order_evaluate')->where('id',$id)->update(['delete_time' => date('Y-m-d H:i:s',time()),'is_delete' => 2,'delete_id' => session('manage_id')]);
            Db::name('order_evaluate')->where('id',$id)->delete();
            $title = Db::name('web')->where('id',$id)->find();
            //Db::name('recycle_bin')->insert(['title' => $title['cat_name'],'type' =>'Web管理','table' => 'news','table_id' => $id,'delete_id' => Session::get('manage_id'),'delete_time' => date('Y-m-d H:i:s',time())]);
            win('操作成功', Url::build('index'));
        }
        fail('操作失败');

    }
    /**
     * 栏目状态改变
     */
    public function status($id, Request $request, Model $model)
    {
        $id = $request->instance()->param('id');

        $status = $model->where('id', $id)->value('status') == '1' ? 2 : 1;

        if ($model->where('id', $id)->update(['status' => $status])) {
            return $status;
        }
        return false;
    }


    /**
     * 栏目全部删除
     */
    public function destroyAll($id, AdTypeModel $adType)
    {

        if ($adType->amputateAll($id)) {

            win('操作成功', Url::build('AdType/index'));
        }

        fail('操作失败');

    }





    /*
    * 排列顺序
    */
    public function sorting()
    {

        $id = Request::instance()->param('orderId/a');
        $sort = Request::instance()->param('sort/a');

        foreach ($id as $key => $value) {
            (new AdTypeModel)->where('id', $value)->update(['sort' => $sort[$key]]);
        }

        win('操作成功', Url::build('AdType/index'));
    }

    /**
     * 清除缓存
     */
    protected function clearCache()
    {
        Cache::rm('AdType');
    }
}